//
//  StartTransitionView.m
//  HanYu55like
//
//  Created by junseek on 15-5-20.
//  Copyright (c) 2015年 五五来客 lj. All rights reserved.
//

#import "StartTransitionView.h"

@interface StartTransitionView()<UIScrollViewDelegate>{
    
    UIScrollView *bgView;
    BOOL hidden_self;
    NSArray *arrayImage;
    UIImageView *imageTempV;
    BOOL is_hidden;
    UIPageControl *pagecontroller;
}

@end


@implementation StartTransitionView
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];//
        
        
        [self loadHeadData];
    }
    return self;
    
}


-(void)loadHeadData{
   
    bgView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,kVersion7? 0:20, kScreenWidth, kVersion7?kScreenHeight:kScreenHeight-20)];
    [bgView setShowsHorizontalScrollIndicator:NO];
    [bgView setBackgroundColor:[UIColor clearColor]];
    [bgView setPagingEnabled:YES];
    [bgView setDelegate:self];
    [self addSubview:bgView];
    arrayImage=@[@"guide01",@"guide02",@"guide03"];
    
    for (int i=0; i<[arrayImage count]; i++) {
        UIImageView *imageV=[RHMethods imageviewWithFrame:CGRectMake(i*W(bgView), 0, W(bgView), H(bgView)) defaultimage:[arrayImage objectAtIndex:i]];
        imageV.contentMode=UIViewContentModeScaleAspectFill;
        imageV.clipsToBounds=YES;
        [bgView addSubview:imageV];
        
        if (i==[arrayImage count]-1) {
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenGuide)];
            [imageV setUserInteractionEnabled:YES];
            [imageV addGestureRecognizer:tap];
        }
    }
    bgView.hidden=YES;
    
    pagecontroller = [[UIPageControl alloc]initWithFrame:CGRectMake(0, YH(bgView) - 30, W(bgView), 20)];//(H(bgView)>500?30:20)
    pagecontroller.currentPageIndicatorTintColor =rgbpublicColor;
    pagecontroller.pageIndicatorTintColor =  RGBACOLOR(23, 180,  235, 0.3);
    pagecontroller.currentPage = 0;
    pagecontroller.numberOfPages = arrayImage.count;
    [self addSubview:pagecontroller];
    pagecontroller.hidden=YES;
    
    
    imageTempV=[RHMethods imageviewWithFrame:bgView.frame defaultimage:@"xkfkp" contentMode:UIViewContentModeScaleAspectFit];
    [self addSubview:imageTempV];
    imageTempV.backgroundColor=[UIColor whiteColor];
    imageTempV.hidden=YES;
//    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenGuide)];
//    [imageTempV setUserInteractionEnabled:YES];
//    [imageTempV addGestureRecognizer:tap];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
     float fx=scrollView.contentOffset.x;
    if (fx>([arrayImage count] -1)*kScreenWidth+50) {
        if (is_hidden) {
            is_hidden=NO;
            [self hiddenGuide];
        }
    }
    float ftw= W(scrollView);
    pagecontroller.currentPage=(fx +ftw/2.0)/ftw;
    
}
///引导页
-(void)showGuide{
    imageTempV.hidden=YES;
    pagecontroller.hidden=YES;
    bgView.hidden=NO;
    hidden_self=NO;
    is_hidden=YES;
    
    [bgView setContentSize:CGSizeMake(W(bgView) * [arrayImage count], H(bgView))];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    
    self.hidden = NO;
    self.alpha = 1.0f;
    
}

-(void)hiddenGuide{
    [UIView animateWithDuration:0.4 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        self.hidden=YES;
    }];
}

- (void)show
{
    is_hidden=NO;
    hidden_self=YES;
    imageTempV.hidden=NO;
    
    pagecontroller.hidden=YES;
    bgView.hidden=YES;
//    [bgView setContentSize:CGSizeMake(W(bgView) * arrayImage.count, H(bgView))];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    
    self.hidden = NO;
    self.alpha = 1.0f;
    
//    [UIView animateWithDuration:0.3
//                          delay:0.0
//                        options:UIViewAnimationOptionCurveEaseOut
//                     animations:^{
//                         self.alpha = 1.0f;
//                         
//                     }
//                     completion:^(BOOL finished) {
//                         
//                     }];
    
}

- (void)hidden
{
    if (hidden_self) {
        [UIView animateWithDuration:0.4 animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            self.hidden=YES;
        }];

    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
